/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.entitybeans.systemacc;

import com.bionic.ce.entities.Currency;
import com.bionic.ce.entities.Systemacc;
import com.bionic.ce.entities.User;
import com.bionic.ce.entitybeans.AbstractFacade;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Jarik
 */
@Stateless
public class SystemaccFacade extends AbstractFacade<Systemacc> implements SystemaccFacadeLocal {

    @PersistenceContext(unitName = "Currency_ExchangeEE-PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public SystemaccFacade() {
        super(Systemacc.class);
    }

    public Systemacc findById(Integer id) {
        TypedQuery<Systemacc> query = em.createNamedQuery("Systemacc.findByAccNum", Systemacc.class);
        query.setParameter("accNum", id);
        Systemacc systemAcc = null;
        try {
            systemAcc = query.getSingleResult();
        } catch (NoResultException e) {
            e.printStackTrace();
        }
        return systemAcc;
    }

    @Override
    public List<Systemacc> findByUserIdAndCurrency(Integer userId, String currency) {
        String queryString = "SELECT s FROM Systemacc s WHERE userid=" + userId;
        String addQueryCurrency = " AND currency=\'" + currency + "\'";
        if (currency != null) {
            queryString += addQueryCurrency;
        }
        TypedQuery<Systemacc> query = em.createQuery(queryString, Systemacc.class);

        List<Systemacc> systemacclist = null;
        try {
            systemacclist = query.getResultList();
        } catch (NoResultException e) {
            e.printStackTrace();
        }
        return systemacclist;
    }

    public void executedQueryMoneyTransfer(User userFrom, Currency currencyFrom, Currency currencyTo, User userTo, Double queryAmount, Double exchangedAmount) {

        Systemacc userFromAccountFrom = (Systemacc)(findByUserIdAndCurrency(userFrom.getId(), currencyFrom.getShortName()).toArray())[0];
        Systemacc userFromAccountTo = (Systemacc) (findByUserIdAndCurrency(userFrom.getId(), currencyTo.getShortName()).toArray())[0];
        Systemacc userToAccountFrom = (Systemacc) (findByUserIdAndCurrency(userTo.getId(), currencyFrom.getShortName()).toArray())[0];
        Systemacc userToAccountTo = (Systemacc)(findByUserIdAndCurrency(userTo.getId(), currencyTo.getShortName()).toArray())[0];

        userFromAccountFrom.setAmount(roundResult(userFromAccountFrom.getAmount() - queryAmount, 2));
        userFromAccountTo.setAmount(userFromAccountTo.getAmount() + exchangedAmount);
        userToAccountFrom.setAmount(userToAccountFrom.getAmount() + queryAmount);
        userToAccountTo.setAmount(roundResult(userToAccountTo.getAmount() - exchangedAmount, 2));
        em.merge(userFromAccountFrom);
        em.merge(userFromAccountTo);
        em.merge(userToAccountFrom);
        em.merge(userToAccountTo);
    }

    private static double roundResult(double d, int precise) {
        precise = 10 ^ precise;
        d = d * precise;
        int i = (int) Math.round(d);
        return (double) i / precise;
    }
}
